<?php

/**
 * 商品管理
 */

namespace app\admin\controller;
use think\Lang;
use think\Db;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Producty extends AdminControl {

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/product.lang.php');
    }

    /**
     * 商品管理
     */
    public function index() {
        $goods_model = model('goods');
      /*  $list =$goods_model->getGoodsUnionList('', '*', 'goodscommon.goods_verify desc, goodscommon.goods_commonid desc','',10);*/


        /**
         * 处理商品分类
         */
        $choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
        $gccache_arr = model('goodsclass')->getGoodsclassCache($choose_gcid, 3);
        $this->assign('gc_json', json_encode($gccache_arr['showclass']));
        $this->assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));

        /**
         * 查询条件
         */
        $where = array();
        $where['goodscommon.is_two_goods'] = 0;
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['goods.goods_name'] = array('like', '%' . $search_goods_name . '%');
        }

        // 审核状态条件
        $verify_where = input('param.goods_verify');
        if ($verify_where != '') {
            $where['goods.goods_verify'] = $verify_where;
        }


        $search_store_name = trim(input('param.search_store_name'));
        if ($search_store_name != '') {
            $where['y_goods.store_name'] = array('like', '%' .$search_store_name . '%');
            //$where['y_goods.store_id'] = $search_store_name;
        }
        $search_store_id = trim(input('param.search_store_id'));
        if ($search_store_id != '') {
            $where['y_goods.id'] = $search_store_id;
        }
        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['goodscommon.brand_id'] = $b_id;
        }
        if ($choose_gcid > 0) {
            $where['goodscommon.gc_id_' . ($gccache_arr['showclass'][$choose_gcid]['depth'])] = $choose_gcid;
        }
//        $where['goodscommon.is_temlate'] = 0;  #是否是模板
        $type = input('param.type');
        $where['goodscommon.goods_lock'] = 0; #商品没有被删除
        $search_commonid = intval(input('param.search_commonid'));
        if ($search_commonid > 0) {
            $where['goodscommon.goods_commonid'] = $search_commonid;
        }
        #未删除商品
        $where['y_goods.is_del'] = 0;
        $goods_lists = []; // 防止报错，错误原因未定义这个变量

        switch ($type) {
            case 'status_up':
                #在售商品
                $where['goodscommon.goods_status'] = 1;
                //p($where);die;
                $goods_list = $goods_model->getGoodsUnionLists($where, '*', 'goodscommon.goods_verify desc, goodscommon.goods_commonid desc','',10 );
                break;
            // 全部商品
            default:
             #筛选商品
        // $where['goodscommon.goods_status'] = array('in','1,2,4');
        // p($where);die;
                $where['goodscommon.goods_status'] = 1;
               $goods_list = $goods_model->getGoodsUnionLists($where, '*,goods.goods_name,store.store_name as new_name,y_goods.add_time as imports_time', 'goodscommon.mall_goods_commend desc,goodscommon.mall_goods_sort asc','',10 );
              //获取分类名称
              $goods_lists = getClassName($goods_list);

                break;
        }
       // p($goods_list);die;
        $this->assign('goods_list', $goods_lists);
        $this->assign('show_page', $goods_model->page_info->render());

        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());

        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        $this->assign('state', array('1' => lang('goods_state_1'), '0' => lang('goods_state_0'), '10' => lang('goods_state_10'), '100' => '审核中'));

        $this->assign('verify', array('0' => '通过', '1' => '未通过', '10' => '审核中'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if(!in_array($type, array('lockup','waitverify','allgoods'))){
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem($type);
        return $this->fetch();
    }


      #上下架
     public function goods_status(){
    $id = input('param.id');

    $goods_info = db('y_goods')->where('id',$id)->find();
    if (empty($goods_info)) {
        ds_json_encode(10001,'操作失败');
        }
     if($goods_info['status']==0){
        #下架操作
        $goods_status = 1;
     }else{
        #上架操作
         $goods_status = 0;
      }
         $return = db('y_goods')->where('id',$id)->data(['status'=>$goods_status])->update();
         if ($return) {
             // 添加操作日志
             $this->log('运营商商品上下架id'.$id, 1);
             ds_json_encode(10000,'操作成功');
         } else {
             $this->log('运营商商品上下架id'.$id, 0);
             ds_json_encode(10001,'操作失败');
         }

     }



    //ajax获取同一个commonid下面的商品信息
    public function get_goods_list_ajax() {
        $common_id = input('param.commonid');
        if (empty($common_id)) {
            $this->error(lang('param_error'));
        }
        $map['goods_commonid'] = $common_id;
        $goods_model = model('goods');
        $common_info = $goods_model->getGoodeCommonInfo($map,'spec_name');
        $goods_list = $goods_model->getGoodsList($map);
        //halt($goods_list);
        $spec_name = array_values((array) unserialize($common_info['spec_name']));
        foreach ($goods_list as $key => $val) {
            $goods_spec = array_values((array) unserialize($val['goods_spec']));
            $spec_array = array();
            foreach ($goods_spec as $k => $v) {
                $spec_array[] = '<div class="goods_spec">' . $spec_name[$k] . ':' . '<em title="' . $v . '">' . $v . '</em>' . '</div>';
            }
            $goods_list[$key]['goods_image'] = goods_cthumb($val['goods_image']);
            $goods_list[$key]['goods_spec'] = implode('', $spec_array);
            $goods_list[$key]['url'] = url('Home/Goods/index', array('goods_id' => $val['goods_id']));
        }
        return json_encode($goods_list);
    }

    /**
     * ajax操作
     */
    public function ajax() {
        $goods_model = model('goods');
        switch (input('param.branch')) {
            case 'mall_goods_commend':
            case 'mall_goods_sort':
                if (empty($result)) {
                    $goods_model->editGoodsCommonById(array(trim(input('param.branch')) => trim(input('param.value'))),array(intval(input('param.id'))));
                    echo 'true';
                    exit;
                } else {
                    echo 'false';
                    exit;
                }
                break;
        }
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'allgoods',
                'text' => '在售商品',
                'url' => url('Producty/index')
            ),
          /*  array(
                'name' => 'lockup',
                'text' => lang('goods_index_lock_goods'),
                'url' => url('Producty/index', ['type' => 'lockup'])
            ),
            array(
                'name' => 'waitverify',
                'text' => lang('goods_index_waitverify_goods'),
                'url' => url('Producty/index', ['type' => 'waitverify'])
            ),*/
        );
        return $menu_array;
    }

}

?>
